﻿using Cl.Utils.Extention;
using Cl.Utils.Helper.App;
using Cl.Utils.Primitives.Class;
using Cl.Utils.Primitives.Class.Systems;
using Cl.Utils.Primitives.Class.UploadFile;
using Lims.Util.Primitives.Class.Systems;
using Microsoft.Extensions.Configuration;
using System;
using System.Configuration;
namespace Cl.Utils.Helper
{
    public static class ConfigHelper
    {
        private static IConfiguration Configuration;

        public static void Initialize(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        /// <summary>
        /// 获取配置
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetSection(string key)
        {
            return Configuration.GetValue<string>(key);
        }

        /// <summary>
        /// 获取集合配置
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string[] GetSections(string key)
        {
            return (string[])Configuration.GetSection(key).Get(typeof(string[]));
        }


        public static FileConfig GetFileConfig()
        {
            var result = new FileConfig
            {
                EnableCustomFolder = GetSection("FileConfig:EnableCustomFolder").ToLimsBool(),
                RequestPath = GetSection("FileConfig:RequestPath"),
                DateTimeDirectory = GetSection("FileConfig:DateTimeDirectory"),
                ExcludeExtension = GetSections("FileConfig:ExcludeExtension").ToLimsStrList(),
                IncludeExtension = GetSections("FileConfig:IncludeExtension").ToLimsStrList(),
                MaxSize = GetSection("FileConfig:MaxSize").ToLimsInt32(),
                Md5 = GetSection("FileConfig:Md5").ToLimsBool(),
                UploadMaxCount = GetSection("FileConfig:UploadMaxCount").ToLimsInt32(),
            };

            if (result.EnableCustomFolder)
                result.UploadPath = GetSection("FileConfig:UploadPath");
            else
                result.UploadPath = $"{Appdir.AppRoot}\\Upload";
            return result;
        }

        public static SystemConfig GetSystemConfigs()
        {
            return new SystemConfig
            {
                EnableSwaggerUI = GetSection("SystemConfigs:EnableSwaggerUI").ToLimsBool(),
                EnableElsaUI = GetSection("SystemConfigs:EnableElsaUI").ToLimsBool(),
                EnableSafeMode = GetSection("SystemConfigs:EnableSafeMode").ToLimsBool(),
            };
        }

        /// <summary>
        /// 获取上传路径
        /// </summary>
        /// <returns></returns>
        public static string GetUploadTempPath()
        {
            return GetSection("shareFilePaths:uploadFileTemp");
        }

        /// <summary>
        /// 获取JWT配置
        /// </summary>
        /// <returns></returns>
        public static JWTConfig GetJwtConfig()
        {
            return new JWTConfig
            {
                Audience = GetSection("JWT:Audience"),
                Issuer = GetSection("JWT:Issuer"),
                Secret = GetSection("JWT:SecretKey"),
                Expires = Convert.ToInt32(GetSection("JWT:Expires")),
            };
        }

        public static IConfigurationSection GetConfigurationSection(string key)
        {
            return Configuration.GetSection(key);
        }

        internal static DefaultDataDto GetDefaultData()
        {
            return new DefaultDataDto
            {
                DefaultUserCode = GetSection("DefaultData:UserCode"),
                DefaultRoleName = GetSection("DefaultData:RoleName"),
                CustomerRoleName = GetSection("DefaultData:CustomerRoleName"),
            };
        }

        public static RedisServiceConfigDto GetRedisServiceConfig()
        {
            return new RedisServiceConfigDto
            {
                Enable = GetSection("RedisService:Enable").ToLimsBool(),
                ConnectionString = GetSection("RedisService:ConnectionString"),
                Password = GetSection("RedisService:ConnectionString"),
                DatabaseId = GetSection("RedisService:DatabaseId").ToLimsInt32(),
                ConnectTimeout = GetSection("RedisService:ConnectTimeout").ToLimsInt32(),
                SyncTimeout = GetSection("RedisService:SyncTimeout").ToLimsInt32(),
            };
        }
    }
}
